import logging

from strands.models.openai import OpenAIModel

logger = logging.getLogger(__name__)


class LlmFactory:

    @staticmethod
    def openai_model(
        openai_key: str,
        model_id: str = "gpt-4.1-mini",
        max_completion_tokens: int = 4096,
        temp: float = 0.7,
        top_p: float = 1.0,
    ) -> OpenAIModel:
        """
        models:
        gpt-4.1, gpt-4o, gpt-4o-mini, gpt-4.1-mini, gpt-3.5-turbo
        """

        logger.info(f"using openai model={model_id} key={openai_key[0:8]}")
        model = OpenAIModel(
            client_args={
                "api_key": openai_key,
                "max_retries": 3,
                "timeout": 60.0,
            },
            model_id=model_id,
            params={
                "max_completion_tokens": max_completion_tokens,
                "temperature": temp,
                "top_p": top_p,
            },
        )
        return model

    @staticmethod
    def anthropic_model(
        anthropic_key: str,
        model_id: str = "claude-sonnet-4-20250514",
        max_tokens: int = 1024 * 16,
        temp: float = 0.7,
    ):
        """
        models:
        claude-sonnet-4-20250514
        claude-haiku-4-5-20251001
        claude-opus-4-20250514
        """
        from strands.models.anthropic import AnthropicModel

        logger.info(f"using anthropic key {anthropic_key[0:12]}")
        # AGT-04 fix: match OpenAI's timeout/max_retries posture so a stalled
        # Anthropic connection cannot hang the ReAct loop indefinitely.
        model = AnthropicModel(
            client_args={
                "api_key": anthropic_key,
                "timeout": 60.0,
                "max_retries": 3,
            },
            model_id=model_id,
            max_tokens=max_tokens,
            params={
                "temperature": temp,
            },
        )
        return model
